﻿Public Class MASPROVINCE
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection

    Dim PROVIDER_MASPROVINCE As New PROVIDERSCLASS

    Private Sub codeChangwatTextbox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CHANGWATIDTxt.KeyPress
        If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) And e.KeyChar.ToString.Trim <> "." Then
            e.Handled = True
        End If
    End Sub

    Private Sub filterCountry_Click(sender As Object, e As EventArgs) Handles filterCountry.Click
        filterCountry.Text = ""
    End Sub

    Private Sub MASPROVINCE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(codecountry AS CHAR(8)) AS CODECOUNTRY, country FROM mascountry"
        s = New FILTERCLASS(filterCountry, sql, "รหัสประเทศ,ชื่อประเทศ", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

        selectDataGridChangwat()

        DesignForm(Me)
        DesignGrid(MASCHANGWATGrid)

        PROVIDER_MASPROVINCE.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
        connect.Dispose()
        'connectlocal.Dispose()
    End Sub

    Private Sub selectDataGridChangwat()
        Dim objSelectDatamaschangwat As New prated
        Dim MASChangwat As DataTable
        MASChangwat = connect.GetTable(objSelectDatamaschangwat.selectChangwat_)
        MASCHANGWATGrid.PrimaryGrid.DataSource = MASChangwat
    End Sub

    Private Sub MASCHANGWATGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASCHANGWATGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสจังหวัด").Value() IsNot DBNull.Value Then
            CHANGWATIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสจังหวัด").Value()
        Else
            CHANGWATIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value() IsNot DBNull.Value Then
            CHANGWATTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value()
        Else
            CHANGWATTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value() IsNot DBNull.Value Then
            filterCountry.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value()
            filterCountry.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECOUNTRY1").Value()
        Else
            filterCountry.Text = ""
            filterCountry.Tag = ""
        End If
        addChangwat.Enabled = False
    End Sub

    Private Sub addChangwat_Click_1(sender As Object, e As EventArgs) Handles addChangwat.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASPROVINCE.PADD_ = True Then
                If filterCountry.Tag.ToString.Length > 0 And CHANGWATTxt.Text.Length > 0 And CHANGWATIDTxt.Text.Length > 0 Then
                    'connectlocal = conlocal.NewConnection
                    Dim objInsertData As New prated
                    objInsertData.changwatCODECHANGWAT_ = CInt(CHANGWATIDTxt.Text)
                    objInsertData.changwatCHANGWAT_ = CHANGWATTxt.Text
                    objInsertData.changwatCODECOUNTRY1_ = filterCountry.Tag
                    objInsertData.insertChangwat()

                    selectDataGridChangwat()

                    'connectlocal.Dispose()
                    CHANGWATIDTxt.Text = ""
                    CHANGWATTxt.Text = ""
                    filterCountry.Text = ""
                    filterCountry.Tag = ""
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนทุกช่องก่อนทำการเพิ่มข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_MASPROVINCE.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editChangwat_Click_1(sender As Object, e As EventArgs) Handles editChangwat.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASPROVINCE.PUPD_ = True Then
                If filterCountry.Tag.ToString.Length > 0 And CHANGWATTxt.Text.Length > 0 And CHANGWATIDTxt.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขจังหวัดดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        'connectlocal = conlocal.NewConnection
                        Dim objEditData As New prated
                        objEditData.changwatCODECHANGWAT_ = CInt(CHANGWATIDTxt.Text)
                        objEditData.changwatCHANGWAT_ = CHANGWATTxt.Text
                        objEditData.changwatCODECOUNTRY1_ = filterCountry.Tag
                        objEditData.editChangwat()

                        selectDataGridChangwat()

                        CHANGWATIDTxt.Text = ""
                        CHANGWATTxt.Text = ""
                        filterCountry.Text = ""
                        filterCountry.Tag = ""
                    End If
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
                End If
                addChangwat.Enabled = True
            Else
                MsgBox(PROVIDER_MASPROVINCE.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL("maschangwat")
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL("maschangwat")
        nextform.ShowDialog()
    End Sub
End Class